package com.minelx.scriptbuilder.parse.parser;

import com.minelx.scriptbuilder.script.attribute.function.Params;
import com.minelx.sequence.IStringScanner;
import com.minelx.sequence.ReturnMatcher;
import com.minelx.scriptbuilder.script.IVariable;
import com.minelx.scriptbuilder.script.ScriptContext;

public class ParamsParser {

	private final ValuesParser valuesParser;

	private final ReturnMatcher<IVariable, IStringScanner> paramParser;

	public ParamsParser(ScriptContext context) {
		valuesParser = new ValuesParser(context);

		paramParser = IStringScanner.match(
				IStringScanner.match(","),
				ReturnMatcher.alwaysMatch(valuesParser::parse));
	}

	public Params parse(IStringScanner scanner) {
		Params result = new Params();
		IVariable firstValue = valuesParser.parse(scanner);
		if (firstValue.isNull()) {
			return result;
		}
		result.append(firstValue);
		scanner.sequence(paramParser).forEach(result::append);
		return result;
	}
}
